package com.kaisa.activity.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.kaisa.activity.R;
import com.kaisa.activity.adapter.SearchAdapter;
import com.kaisa.activity.bean.SearchBean;
import com.kaisa.activity.bean.SearchDetails;
import com.kaisa.activity.constant.UrlHttp;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class SearchCountriesActivity extends AppCompatActivity {
    public static final String TOAST_STR = "抱歉，根据您的条件未能找到相关产品路线，请重置条件";

    @ViewInject(R.id.lv_search)
    private PullToRefreshListView searchListView;
    @ViewInject(R.id.iv_secrch_back)
    private ImageView back;

    private String queryKey;

    private SearchBean bean;
    private List<SearchDetails> list = new ArrayList<SearchDetails>();
    private SearchAdapter adapter;
    private int tag = 0;
    private int pn = 0;

    private PopupWindow pop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_countries);
        x.view().inject(this);
        Intent intent = getIntent();
        queryKey = intent.getStringExtra("queryKey");

        initView();
        initData();
        addListener();
    }

    private void addListener() {
        //滑到低端的监听
        searchListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                initData();
            }
        });

        //list点击监听
        searchListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(SearchCountriesActivity.this,ProductDetailsActivity.class);
                intent.putExtra("entity_id",list.get(i-1).getEntityId());
                intent.putExtra("tourGroupId",list.get(i-1).getLowestpriceGroupid());
                startActivity(intent);
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initView() {
        searchListView = (PullToRefreshListView) this.findViewById(R.id.lv_search);
        adapter = new SearchAdapter(list,this);
        searchListView.setAdapter(adapter);

    }

    private void initData() {
        x.http().get(getParams(), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                bean = JSONObject.parseObject(s, SearchBean.class);
                list.addAll(bean.getList());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {
                searchListView.onRefreshComplete();
                if (list.size() == Integer.valueOf(bean.getTotalFound())) {
                    tag++;
                    if (tag > 1) {
                        Toast.makeText(SearchCountriesActivity.this, TOAST_STR, Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    public RequestParams getParams() {
        pn++;


        RequestParams params = new RequestParams(UrlHttp.PATH_SEARCH_CON);

        params.addBodyParameter("queryKey",queryKey);

        params.addBodyParameter("companyid","178");
        params.addBodyParameter("ps","10");
        params.addBodyParameter("pn",pn+"");
        params.addBodyParameter("account", "123");
        params.addBodyParameter("channel", "CS0001");
        params.addBodyParameter("password", "123");
        params.addBodyParameter("channelSecond", "a0002");
        params.addBodyParameter("terminal", "android");
        params.addBodyParameter("sourceFlag", "860806020832282");
        return params;
    }
}
